package com.ruoyi.mes.utils;

import com.ruoyi.common.utils.DateUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 编码自动生成工具类
 */

//public class CodeUtils {
//
//    private static LocalDate date = LocalDate.now();
//
//    private static int progCode = 1;//生产计划编号
//    private static int mtrPlanCode = 1;//物料计划编号
//    private static int buyNeedCode = 1;//采购需求编号
//    private static int buyOrderCode = 1;//采购需求编号
//    private static int wkspCode;//车间任务编号
//    private static int pickCode = 1;//领料编号
//
//    private static final String PREFIX = "PL-";//生产计划前缀
//    private static final String MTRPLAN = "MRP-";//物料计划前缀
//    private static final String BUYNEED = "BN-";//采购需求前缀
//    private static final String BUYORDER = "BO-";//采购订单前缀
//    private static final String WKSPTASK = "RW-";//车间任务前缀
//    private static final String PICK = "LL-";//车间任务前缀
//
//    private static final String SEPARATOR = "-";
//    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyMMdd");
//
//    /**
//     * 根据当前时间生成 生产计划编号
//     *
//     * @return
//     */
//    public static String ProgCodeCreat() {
//        if (date == null) {
//            throw new IllegalArgumentException("Date must not be null.");
//        }
//        StringBuilder builder = new StringBuilder(PREFIX);
//        builder.append(date.format(FORMATTER));
//        builder.append(SEPARATOR);
//        builder.append(progCode++);
//        return builder.toString();
//    }
//
//    /**
//     * 根据当前时间生成 物料计划编号
//     *
//     * @return
//     */
//    public static String MtrPlanCodeCreat() {
//        if (date == null) {
//            throw new IllegalArgumentException("Date must not be null.");
//        }
//        StringBuilder builder = new StringBuilder(MTRPLAN);
//        builder.append(date.format(FORMATTER));
//        builder.append(SEPARATOR);
//        builder.append(mtrPlanCode++);
//        return builder.toString();
//    }
//
//    /**
//     * 根据当前时间生成 采购需求编号
//     *
//     * @return
//     */
//    public static String BuyNeedCodeCreat() {
//        if (date == null) {
//            throw new IllegalArgumentException("Date must not be null.");
//        }
//        StringBuilder builder = new StringBuilder(BUYNEED);
//        builder.append(date.format(FORMATTER));
//        builder.append(SEPARATOR);
//        builder.append(buyNeedCode++);
//        return builder.toString();
//    }
//
//    /**
//     * 根据当前时间生成 采购需求编号
//     *
//     * @return
//     */
//    public static String BuyOrderCodeCreat() {
//        if (date == null) {
//            throw new IllegalArgumentException("Date must not be null.");
//        }
//        StringBuilder builder = new StringBuilder(BUYORDER);
//        builder.append(date.format(FORMATTER));
//        builder.append(SEPARATOR);
//        builder.append(buyOrderCode++);
//        return builder.toString();
//    }
//
//    /**
//     * 根据当前时间生成 车间任务编号
//     *
//     * @return
//     */
//    public static String WkspCodeCreat() {
//        if (date == null) {
//            throw new IllegalArgumentException("Date must not be null.");
//        }
//        StringBuilder builder = new StringBuilder(WKSPTASK);
//        builder.append(date.format(FORMATTER));
//        builder.append(SEPARATOR);
//        builder.append(wkspCode++);
//        return builder.toString();
//    }
//
//    /**
//     * 根据当前时间生成 领料编号
//     *
//     * @return
//     */
//    public static String PickCodeCreat() {
//        if (date == null) {
//            throw new IllegalArgumentException("Date must not be null.");
//        }
//        StringBuilder builder = new StringBuilder(PICK);
//        builder.append(date.format(FORMATTER));
//        builder.append(SEPARATOR);
//        builder.append(pickCode++);
//        return builder.toString();
//    }
//}


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 编码自动生成工具类
 */
public class CodeUtils {

    private static final AtomicInteger progCode = new AtomicInteger(1); // 生产计划编号计数器
    private static final AtomicInteger mtrPlanCode = new AtomicInteger(1); // 物料计划编号计数器
    private static final AtomicInteger buyNeedCode = new AtomicInteger(1); // 采购需求编号计数器
    private static final AtomicInteger buyOrderCode = new AtomicInteger(1); // 采购订单编号计数器
    private static final AtomicInteger wkspCode = new AtomicInteger(1); // 车间任务编号计数器
    private static final AtomicInteger pickCode = new AtomicInteger(1); // 领料编号计数器

    private static final String PREFIX = "PL-"; // 生产计划前缀
    private static final String MTRPLAN = "MRP-"; // 物料计划前缀
    private static final String BUYNEED = "BN-"; // 采购需求前缀
    private static final String BUYORDER = "BO-"; // 采购订单前缀
    private static final String WKSPTASK = "RW-"; // 车间任务前缀
    private static final String PICK = "LL-"; // 领料编号前缀

    private static final String SEPARATOR = "-";
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyMMddHHmmss");

    /**
     * 根据当前时间生成生产计划编号
     *
     * @return 唯一的生产计划编号
     */
    public static String ProgCodeCreat() {
        return createCode(PREFIX, progCode);
    }

    /**
     * 根据当前时间生成物料计划编号
     *
     * @return 唯一的物料计划编号
     */
    public static String MtrPlanCodeCreat() {
        return createCode(MTRPLAN, mtrPlanCode);
    }

    /**
     * 根据当前时间生成采购需求编号
     *
     * @return 唯一的采购需求编号
     */
    public static String BuyNeedCodeCreat() {
        return createCode(BUYNEED, buyNeedCode);
    }

    /**
     * 根据当前时间生成采购订单编号
     *
     * @return 唯一的采购订单编号
     */
    public static String BuyOrderCodeCreat() {
        return createCode(BUYORDER, buyOrderCode);
    }

    /**
     * 根据当前时间生成车间任务编号
     *
     * @return 唯一的车间任务编号
     */
    public static String WkspCodeCreat() {
        return createCode(WKSPTASK, wkspCode);
    }

    /**
     * 根据当前时间生成领料编号
     *
     * @return 唯一的领料编号
     */
    public static String PickCodeCreat() {
        return createCode(PICK, pickCode);
    }

    /**
     * 通用编号生成逻辑
     *
     * @param prefix  前缀
     * @param counter 自增计数器
     * @return 唯一编号
     */
    private static String createCode(String prefix, AtomicInteger counter) {
        LocalDateTime now = LocalDateTime.now();
        StringBuilder builder = new StringBuilder(prefix);
        builder.append(now.format(FORMATTER));
        builder.append(SEPARATOR);
        builder.append(counter.getAndIncrement());
        return builder.toString();
    }

    public static void main(String[] args) {
        // 示例测试
        System.out.println(ProgCodeCreat());
        System.out.println(MtrPlanCodeCreat());
        System.out.println(BuyNeedCodeCreat());
        System.out.println(BuyOrderCodeCreat());
        System.out.println(WkspCodeCreat());
        System.out.println(PickCodeCreat());
    }
}
